package com.pengxun.manager.controller.system;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.validation.Valid;

import org.apache.commons.lang3.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.pengxun.manager.model.system.Menu;
import com.pengxun.manager.service.system.LoginService;
import com.pengxun.manager.service.system.MenuService;
import com.szeastroc.common.exception.EastrocException;
import com.szeastroc.common.jdbc.page.Page;
import com.szeastroc.common.result.Result;
import com.szeastroc.common.result.ResultUtils;

@RestController
@RequestMapping("/menu")
public class MenuController {

    @Resource
    private MenuService menuService;
    @Resource
    LoginService loginService;

    @PostMapping(value = "/insert")
    public Result insert(@Valid @RequestBody Menu menu, BindingResult result) {
        try {
            if (result.hasErrors()) {
                throw new EastrocException(result.getFieldError().getDefaultMessage());
            }
            Date date = new Date();
            menu.setCreateDate(date);
            menu.setCreateBy(loginService.getCurrentUser().getUserId());
            menu.setUpdateDate(date);
            menu.setUpdateBy(loginService.getCurrentUser().getUserId());
            menuService.insert(menu);
        } catch (Exception e) {
            return ResultUtils.renderError(e.getMessage());
        }
        return ResultUtils.render();
    }

    ;

    @PostMapping("/update")
    public Result update(@Valid @RequestBody Menu menu, BindingResult result) {
        try {
            if (result.hasErrors()) {
                throw new EastrocException(result.getFieldError().getDefaultMessage());
            }
            Date date = new Date();
            menu.setUpdateDate(date);
            menu.setUpdateBy(loginService.getCurrentUser().getUserId());
            menuService.update(menu);
        } catch (Exception e) {
            return ResultUtils.renderError(e.getMessage());
        }
        return ResultUtils.render();
    }

    @RequestMapping("/delete")
    public Result delete(Integer menuId) {
        try {
            menuService.delete(menuId);
        } catch (Exception e) {
            return ResultUtils.renderError(e.getMessage());
        }
        return ResultUtils.render();
    }

    ;

    @RequestMapping("/findByPage")
    public Result findByPage(Page page, String menuName) {
        try {
            page = menuService.findByPage(page, menuName);
        } catch (Exception e) {
            return ResultUtils.renderError(e.getMessage());
        }
        return ResultUtils.render(page);
    }

    ;

    @RequestMapping("/findById")
    public Result findById(Integer menuId) {
        try {
            Menu menu = menuService.findById(menuId);
            return ResultUtils.render(menu);
        } catch (Exception e) {
            return ResultUtils.renderError(e.getMessage());
        }
    }

    @RequestMapping("/list")
    public Result findAll(String menuName) {
        try {
            List<Menu> list = new ArrayList<Menu>();
            if (StringUtils.isNotBlank(menuName)) {
                list = menuService.findByMenuName(menuName);
            } else {
                list = menuService.findAll();
            }
            return ResultUtils.render(list);
        } catch (Exception e) {
            return ResultUtils.renderError(e.getMessage());
        }
    }

    @RequestMapping("/findMenuAuthority")
    public Result findMenuAuthority() {
        try {
            JSONObject jsonObject = menuService.findMenuAuthority();
            return ResultUtils.render(jsonObject);
        } catch (Exception e) {
            return ResultUtils.renderError(e.getMessage());
        }
    }

    @RequestMapping("/findMenuTree")
    public Result findMenuTree() {
        try {
            List<Menu> list = menuService.findMenuTree();
            return ResultUtils.render(list);
        } catch (Exception e) {
            return ResultUtils.renderError(e.getMessage());
        }
    }

    @RequestMapping("/updateStatus")
    public Result updateStatus(Integer menuId, Integer status) {
        try {
            menuService.updateStatus(menuId, status);
        } catch (Exception e) {
            return ResultUtils.renderError(e.getMessage());
        }
        return ResultUtils.render();
    }

    ;

    @RequestMapping("/findAllMenu")
    public Result findAllMenu() {
        try {
            List<Menu> list = menuService.findAllMenu();
            return ResultUtils.render(list);
        } catch (Exception e) {
            return ResultUtils.renderError(e.getMessage());
        }
    }
}
